package com.edu.juc.collections.map;

import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArraySet;

public class HashSetTest {
    public static void main(String[] args) {
        // CopyOnWriteArraySet解决HashSet线程不安全问题
//        HashSet<Integer> set = new HashSet<>();
        CopyOnWriteArraySet<Integer> set = new CopyOnWriteArraySet<>();
        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                set.add(i);
            }
            System.out.println(set.size());
        }, "1").start();

        new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                set.add(i);
            }
            System.out.println(set.size());
        }, "2").start();
    }
}
